@echo off
chcp 65001 > nul
color 0B
setlocal enabledelayedexpansion

echo 正在检查端口9291的占用情况...

set "n="
for /f "tokens=5" %%i in ('netstat -aon ^| findstr ":9291"') do (
    set "n=%%i"
)

if defined n (
    echo 找到占用端口9291的进程，PID: !n!
    taskkill /f /pid !n!
    echo 已尝试终止进程 !n!
) else (
    echo 未找到占用端口9291的进程
)

echo 开始构建项目...
dotnet build

cd Net8.Project || goto :error
echo 成功切换到Net8.Project目录

echo 启动应用程序...
start dotnet run --urls=http://localhost:9291

echo 应用程序已在后台启动，访问 http://localhost:9291
echo 按任意键退出...
pause
exit /b 0

:error
echo 执行过程中发生错误！
pause
exit /b 1